﻿using System;
using System.Linq;

namespace RestUpMVC.Resource
{
    internal class ResourceNameResolver : IResourceMetaDataResolver
    {
        public void ResolveMetaData(Type resourceType, ResourceMetaData resourceMetaData)
        {
            if (resourceType == null) throw new ArgumentNullException("resourceType");
            if (resourceMetaData == null) throw new ArgumentNullException("resourceMetaData");

            //first look for the resourcenameattribute
            object[] resourceNameAttribs = resourceType.GetCustomAttributes(typeof (ResourceNameAttribute), false);
            if (resourceNameAttribs.Count().Equals(1))
            {
                resourceMetaData.Name = (resourceNameAttribs[0] as ResourceNameAttribute).Name;
                return;
            }
            resourceMetaData.Name = resourceType.Name;
        }
    }
}